package com.top.up.base;

import com.top.up.enums.ResultEnum;
import lombok.Data;
import lombok.ToString;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Data
@ToString
public class ResultBean<T> {

    // 请求流水号
    private String responseNo;
    private String channelCode;
    private String productCode;
    // 请求时间
    private String requestTime;
    // 请求版本号
    private String version;
    private String code;
    private String message;
    private T data;

    public static class Builder<T>{
        private String responseNo;
        private String channelCode;
        private String productCode;
        private String requestTime;
        private String version;
        private String code;
        private String message;
        private T data;

        public Builder<T> channelCode(String channelCode){
            this.channelCode = channelCode;
            return this;
        }

        public Builder<T> productCode(String productCode){
            this.productCode = productCode;
            return this;
        }

        public Builder<T> responseNo(String responseNo){
            this.responseNo = responseNo;
            return this;
        }

        public Builder<T> fromRequest(RequestBean request){
            this.responseNo = request.getRequestNo();
            this.channelCode = request.getChannelCode();
            this.productCode = request.getProductCode();
            return this;
        }

        public Builder<T> data(T data){
            this.data = data;
            return this;
        }

        public Builder<T> result(ResultEnum e){
            this.code = e.getCode();
            this.message = e.getMsg();
            return this;
        }

        public Builder<T> result(String code, String message){
            this.code = code;
            this.message = message;
            return this;
        }

        public ResultBean<T> successBuild(){
            this.result(ResultEnum.OK);
            this.requestTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            this.version = "V-1.0";
            return new ResultBean<T>(this);
        }

        public ResultBean<T> build(){
            this.requestTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            this.version = "V-1.0";
            return new ResultBean<T>(this);
        }
    }


    private ResultBean(Builder<T> builder){
        this.responseNo = builder.responseNo;
        this.channelCode = builder.channelCode;
        this.productCode = builder.productCode;
        this.requestTime = builder.requestTime;
        this.version = builder.version;
        this.code = builder.code;
        this.message = builder.message;
        this.data = builder.data;
    }
}
